//
//  HWGameCommentSonCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/24.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameCommentSonCell: UITableViewCell {
    var headImageView:UIImageView!
    var headFrameImageView:UIImageView!
    var nameLabel:GranibleLabel!
    var medalImageView:UIImageView!
    var vipImageView:UIImageView!
    var contentLabel:UILabel!
    var timeLabel:UILabel!
    var commentImageView:UIImageView!
    var zanImageView:UIImageView!
    var commentLabel:UILabel!
    var zanLabel:UILabel!
    var commentButton:UIButton!
    var zanButton:UIButton!
    var lineView:UIView!
    var zanButtonClick:((_:String)->())?
    
    var model:HWGameDetailCommentListModel? {
        didSet{
            headImageView.sd_setImage(with: URL(string: model?.touxiang ?? ""), completed: nil)
            headFrameImageView.sd_setImage(with: URL(string: model?.head_frame ?? ""), completed: nil)
            medalImageView.sd_setImage(with: URL(string: model?.medal ?? ""), completed: nil)
            vipImageView.sd_setImage(with: URL(string: model?.huiyuan_img ?? ""), completed: nil)
            contentLabel.text = model?.content
            nameLabel.text = model?.user_name
            timeLabel.text = model?.timetran
            commentLabel.text = model?.num
            zanLabel.text = model?.zan
            if model?.is_zan == true {
                zanImageView.image = UIImage(named: "zan_select")
            }else {
                zanImageView.image = UIImage(named: "zan_normal")
            }
            
            if (model?.color.count ?? 0) > 1 {
                var arr:[UIColor] = Array()
                for color in model?.color ?? [] {
                    arr.append(UIColor.colorWithHexString(hexString: color))
                }
                nameLabel.colors = arr
            }else {
                nameLabel.colors = [UIColor.colorWithHexString(hexString: model?.color[0] ?? "#333333"),UIColor.colorWithHexString(hexString: model?.color[0] ?? "#333333")]
            }
            let replyString = "回复" + (model?.beihuifu_user_name ?? "") + ":" + (model?.content ?? "")
            let attriString:NSMutableAttributedString = NSMutableAttributedString(string:replyString)
            for range in replyString.searchStrRange(model?.beihuifu_user_name ?? "") {
                let colorRange = range
                attriString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.colorWithHexString(hexString: "#F77105"), range: colorRange)
            }
            let style = NSMutableParagraphStyle()
            style.lineSpacing = 5*widthScale
            style.lineBreakMode = NSLineBreakMode.byWordWrapping
            attriString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: NSMakeRange(0, attriString.length))
            contentLabel.attributedText = attriString
        }
    }
    class func cellWithTableView(tableView:UITableView) -> (HWGameCommentSonCell) {
        let reUse:String = "HWGameCommentSonCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameCommentSonCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameCommentSonCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        headImageView = UIImageView()
        headImageView.layer.cornerRadius = 19*widthScale
        headImageView.contentMode = .scaleAspectFill
        headImageView.layer.masksToBounds = true
        contentView.addSubview(headImageView)
        
        nameLabel = GranibleLabel()
        nameLabel.font = .boldSystemFont(ofSize: 16*widthScale)
        contentView.addSubview(nameLabel)
        
        medalImageView = UIImageView.init()
        medalImageView.backgroundColor = .clear
        headImageView.contentMode = .scaleAspectFill
        contentView.addSubview(medalImageView)
        
        vipImageView = UIImageView.init()
        vipImageView.backgroundColor = .clear
        medalImageView.contentMode = .scaleAspectFill
        contentView.addSubview(vipImageView)
        
        headFrameImageView = UIImageView()
        headFrameImageView.backgroundColor = .clear
        headFrameImageView.layer.masksToBounds = true
        headFrameImageView.contentMode = .scaleAspectFill
        contentView.addSubview(headFrameImageView)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#333333", font: 14*widthScale)
        contentView.addSubview(contentLabel)

        timeLabel = HWLabel.createLabel(title: "", color: "#999999", font: 14*widthScale)
        contentView.addSubview(timeLabel)
        
        commentImageView = UIImageView()
        commentImageView.image = UIImage(named: "gameCommentImage")
        contentView.addSubview(commentImageView)
        
        commentLabel = HWLabel.createLabel(title: "", color: "#999999", font: 14*widthScale)
        contentView.addSubview(commentLabel)
        
        zanImageView = UIImageView()
        zanImageView.image = UIImage(named: "zan_normal")
        contentView.addSubview(zanImageView)
        
        zanLabel = HWLabel.createLabel(title: "", color: "#999999", font: 14*widthScale)
        contentView.addSubview(zanLabel)
        
        lineView = UIView()
        lineView.backgroundColor = .colorWithHexString(hexString: "#EBEBEB")
        contentView.addSubview(lineView)
        
        zanButton = UIButton(type: .custom)
        zanButton.addTarget(self, action: #selector(zanButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(zanButton)
    }
    
    func initFrame() -> Void {
        headImageView.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.top.equalTo(0)
            make.width.height.equalTo(38*widthScale)
        }
        
        headFrameImageView.snp.makeConstraints { make in
            make.center.equalTo(headImageView)
            make.width.height.equalTo(50*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(10*widthScale)
//            make.right.equalTo(-15*widthScale)
            make.top.equalTo(headImageView)
        }
        
        medalImageView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(6*widthScale)
            make.centerY.equalTo(nameLabel)
            make.width.height.equalTo(16*widthScale)
        }
        
        vipImageView.snp.makeConstraints { make in
            make.left.equalTo(medalImageView.snp.right).offset(6*widthScale)
            make.centerY.equalTo(medalImageView)
            make.width.height.equalTo(16*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(10*widthScale)
            make.top.equalTo(headImageView.snp.bottom)
            make.right.equalTo(-15*widthScale)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(13*widthScale)
        }
        
        zanImageView.snp.makeConstraints { make in
            make.width.height.equalTo(16*widthScale)
            make.centerY.equalTo(timeLabel)
            make.right.equalTo(zanLabel.snp.left).offset(-5*widthScale)
        }
        
        zanLabel.snp.makeConstraints { make in
            make.right.equalTo(-15*widthScale)
            make.centerY.equalTo(zanImageView)
        }
        
        commentImageView.snp.makeConstraints { make in
            make.width.height.equalTo(16*widthScale)
            make.centerY.equalTo(timeLabel)
            make.right.equalTo(commentLabel.snp.left).offset(-5*widthScale)
        }
        
        commentLabel.snp.makeConstraints { make in
            make.right.equalTo(zanImageView.snp.left).offset(-30*widthScale)
            make.centerY.equalTo(commentImageView)
        }
        
        zanButton.snp.makeConstraints { make in
            make.left.equalTo(zanImageView)
            make.right.equalTo(zanLabel)
            make.top.bottom.equalTo(zanImageView)
        }
        
        lineView.snp.makeConstraints { make in
            make.top.equalTo(timeLabel.snp.bottom).offset(5*widthScale)
            make.left.equalTo(nameLabel)
            make.right.equalTo(-15*widthScale)
            make.height.equalTo(1*widthScale)
            make.bottom.equalTo(-15*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    @objc func zanButtonClick(_ button:UIButton) -> Void {
        self.zanButtonClick?(model?.id ?? "")
    }

}
